/***************************************************************************
 *   Copyright (C) 2007 by Grzegorz Latarowski, Maciej Gajewski            *
 *   g.latarowski@gmail.com                                                *
 *   maciej.gajewski0@gmail.com                                            *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


// Qt
#include <QFile>
#include <QDomDocument>


// local
#include "gexception.h"
#include "gxmldocument.h"




// ===========================================================================
//                                 GXmlDocument
// ===========================================================================


// ---------------------GXmlDocument::writeToStream() ------------------------
void GXmlDocument::writeToStream( QTextStream & out, int indentSize )const
{
		// create xml document 
	QDomDocument doc( documentType() );
		// set content 
	doc.appendChild( this->toXml( doc ) );
		// write document 
	doc.save( out, indentSize );
}
// ---------------------------------------------------------------------------


// ---------------------GXmlDocument::readFromStream() -----------------------
void GXmlDocument::readFromStream( QTextStream & in )
{
		// create document 
	QDomDocument doc;
	QString errorStr;
	int errorLine;
	int errorColumn;
	if ( !doc.setContent( in.device(), true, &errorStr, &errorLine, &errorColumn ) )
	{
		throw GDatasetError( QObject::tr( "Parse error at line %1, column %2:\n%3" )
				.arg( errorLine )
				.arg( errorColumn )
				.arg( errorStr )
						   );
	}
	
	QDomElement element = doc.documentElement();
		// parse data
	this->fromXml( element );
}
// ---------------------------------------------------------------------------


// ---------------------GXmlDocument::saveToFile() ---------------------------
void GXmlDocument::saveToFile( const QString & fileName, int indentSize )const
{
		// open file for writing 
	QFile file( fileName );
	if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
	{
		throw GSysError( QObject::tr( "Can not write to file '%1'." ).arg( fileName ) );
	}

		// create text stream for writing
	QTextStream out( &file );
		// write 
	writeToStream( out, indentSize );
}
// ---------------------------------------------------------------------------


// ---------------------GXmlDocument::loadFromFile() -------------------------
void GXmlDocument::loadFromFile( const QString & fileName )
{
		// open file for reading 
	QFile file( fileName );
	if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
	{
		throw GSysError( QObject::tr( "Can not read from file '%1'." ).arg( fileName ) );
	}

		// create stream on the file 
	QTextStream in( &file );
		// read form the stream 
	readFromStream( in );
}
// ---------------------------------------------------------------------------








// eof
